local jass = require 'jass.common'
local japi = require 'jass.japi'
local dbg = require 'jass.debug'

rawset(japi, 'CreateTextTag', jass['CreateTextTag'])
rawset(japi, 'DestroyTextTag', jass['DestroyTextTag'])
rawset(japi, 'SetTextTagText', jass['SetTextTagText'])
rawset(japi, 'SetTextTagPos', jass['SetTextTagPos'])
rawset(japi, 'SetTextTagPosUnit', jass['SetTextTagPosUnit'])
rawset(japi, 'SetTextTagColor', jass['SetTextTagColor'])
rawset(japi, 'SetTextTagVelocity', jass['SetTextTagVelocity'])
rawset(japi, 'SetTextTagVisibility', jass['SetTextTagVisibility'])
rawset(japi, 'SetTextTagSuspended', jass['SetTextTagSuspended'])
rawset(japi, 'SetTextTagPermanent', jass['SetTextTagPermanent'])
rawset(japi, 'SetTextTagAge', jass['SetTextTagAge'])
rawset(japi, 'SetTextTagLifespan', jass['SetTextTagLifespan'])
rawset(japi, 'SetTextTagFadepoint', jass['SetTextTagFadepoint'])

local texttag = {}
setmetatable(texttag,texttag)
ac.texttag = texttag

-- 输出
function texttag:__tostring()
    return ("漂浮文字[%d]"):format(self.handle)
end 

-- 创建漂浮文字
function texttag:__call(data)
    if not data.target then 
        log.error('没有目标')
        return 
    end 
    local tag = setmetatable(data,texttag)
    tag:init()
    return tag 
end 

-- 模块初始化
function texttag.init()
    texttag.group = {}
    texttag.clock = ac.clock() / 1000
    ac.hardware:on("界面绘制")(function()
        for tag in pairs(texttag.group) do 
            if not tag.removed then 
                tag:update()
            end 
        end
    end)
end 

-- 模块更新
function texttag.update()
    texttag.clock = ac.clock() / 1000
    for tag in pairs(texttag.group) do 
        if not tag.removed and tag.auto then 
            if not tag.target or tag.target.removed then 
                tag:remove()
            end
        end 
    end 
end 

local mt = {}
texttag.__index = mt 

-- 结构类型
mt.type = 'texttag'

-- 句柄(伪)
mt.handle = jass.ConvertUnitState(0)

-- 颜色red
mt.red = 255

-- 颜色green
mt.green = 255

-- 颜色blue
mt.blue = 255

-- 颜色alpha
mt.alpha = 255

-- 文本
mt.text = 'texttag'

-- 字体大小
mt.font_size = 23
mt.jump_rate = nil
mt.jump_time = 0.5
-- 目标
mt.target = nil 

-- 自动删出
mt.auto = true 

mt.angle = 90
mt.speed = 0

-- 刷新 
function mt:update()
    if self.on_update then 
        self:on_update()
    end 
    if self._clock~=-1 and self.jump_rate then
        local time = ac.clock() / 1000 - self._clock
        if time<=self.jump_time then
            local angle = time/self.jump_time * 180
            local ex = math.sin(angle) * self.jump_rate
            self.init_size = self.init_size or self.font_size
            self:set_font_size(self.init_size * (1 + ex))
        end
    end

    if self.target.type == 'unit' then 
        japi.SetTextTagPosUnit(self.handle, self.target.handle, self._height or 0)
    else 
        local x,y = self._point:get()
        japi.SetTextTagPos(self.handle,x,y,self._height)
    end
end 

-- 初始化
function mt:init()
    local handle = japi.CreateTextTag()
    self.handle = handle 
    self.gchash = handle
    self._point = self.target:get_point()
    self._height = self.height or 0 
    dbg.gchash(self,handle)
    texttag.group[self] = true 
    if japi.DzTextTagSetFont then
        japi.DzTextTagSetFont(self.font_path)
    end
    
    self:set_text(self.text)
    self:set_font_size(self.font_size)
    --self:set_color()
    self:move()
    self:set_fade(self.life, self.fade)
    self:show()
    self:update()
    return self 
end 

-- 删除
function mt:remove()
    if self.removed then 
        return 
    end 
    self.removed = true 
    if self._timer then 
        self._timer:remove()
    end
    texttag.group[self] = nil  
    japi.DestroyTextTag(self.handle)
    self.handle = nil
end 

-- 设置可见
function mt:show()
    japi.SetTextTagVisibility(self.handle,true)
end

-- 设置隐藏
function mt:hide()
    japi.SetTextTagVisibility(self.handle,false)
end

-- 设置文本
mt.shadow_color = 0xff000000
function mt:set_text(text)
    self.text = text
    if japi.DzTextTagSetShadowColor then
        japi.DzTextTagSetShadowColor(self.shadow_color)
    end
    japi.SetTextTagText(self.handle,self.text,self.font_size / 1000)
end 


-- 设置字体大小
mt.font_path = 'fonts.ttf'
function mt:set_font_size(font_size)
    self.font_size = font_size or self.font_size
    japi.SetTextTagText(self.handle, self.text, self.font_size * 0.001)
end 

-- 设置颜色
function mt:set_color(...)
    local n = select('#',...)
    if n == 0 then 
        return self.red,self.green,self.blue,self.alpha
    end
    local args = {...}
    self.red   = args[1] or self.red
    self.green = args[2] or self.green
    self.blue  = args[3] or self.blue
    self.alpha = args[4] or self.alpha
    japi.SetTextTagColor(self.handle, math.limit(self.red,0,255), math.limit(self.green,0,255), math.limit(self.blue,0,255), math.limit(self.alpha,0,255))
    return self 
end

-- 设置移动
function mt:move(angle,speed)
    self.angle = angle or self.angle
    self.speed = speed or self.speed
    japi.SetTextTagVelocity(self.handle, self.speed * 0.071 * math.cos(self.angle) / 128, self.speed * 0.071 * math.sin(self.angle) / 128)
    --japi.SetTextTagVelocity(self.handle, self.angle, self.speed / 1000)
    return self 
end 

-- 设置坐标
function mt:get_point()
    return self._point
end 

-- 获取坐标
function mt:set_point(point)
    if point and point.type == 'point' then 
        self._point = point
    else 
        log.error('参数类型错误', point)
    end 
end 

-- 设置高度
function mt:set_height(height)
    if height and type(height) == 'number' then 
        self._height = height
    else 
        log.error('参数类型错误', height)
    end 
end 

-- 获取坐标
function mt:get_height()
    return self._height
end 

-- 设置永久型
function mt:permanent()
    if not self.removed then 
        japi.SetTextTagPermanent(self.handle,true)
        self._life = -1
    end
    return self 
end 

-- 设置淡化时间
function mt:set_fade(lifespan, fadepoint)
    if not self.removed then 
        lifespan = lifespan or 1
        japi.SetTextTagPermanent(self.handle,false)
        japi.SetTextTagLifespan(self.handle, lifespan)
        if fadepoint then 
            japi.SetTextTagFadepoint(self.handle, fadepoint)
        end
        self._clock = ac.clock() / 1000
        self._life = lifespan  
        if self._timer then 
            self._timer:remove()
        end
        self._timer = ac.wait(lifespan*1000,function()
            self:remove()
        end)
    end
    return self 
end

return texttag